//
//  ABSGoodsDetailViewController.m
//  ABSallRec
//
//  Created by 我演示 on 2019/11/6.
//  Copyright © 2019 Jerry-Fu. All rights reserved.
//

#import "ABSGoodsDetailViewController.h"
#import "DetailRootModel.h"
#import "DetailRootBannerModel.h"
#import "ABSGoodsDetailHeaerReusableView.h"
#import "DetailRootMvvmModel.h"
#import "GoodsDetailBottomCartView.h"
#import "GoodsDetailSelectSkuPopView.h"

static NSString *const headerViewIndefine = @"CaseCluessKeybordCollectionHeadView";
static NSString *const footerViewIndefine = @"CaseCluessKeybordCollectionFootView";

static NSString *const CollectionViewCellId = @"Capital_HeaderCollectionViewCellId";


@interface ABSGoodsDetailViewController ()<UICollectionViewDelegate,UICollectionViewDataSource , UICollectionViewDelegateFlowLayout,SDCycleScrollViewDelegate>
/** 数据源信息 */
@property(nonatomic,strong) NSMutableArray *dataSource;

/**创建collectionView*/
@property(nonatomic,strong) UICollectionView *collectionView;

/**bottomView*/
@property(nonatomic,strong) GoodsDetailBottomCartView *bottomView ;

/** SKUvIE*/
/**SKUView*/
@property(nonatomic,strong) GoodsDetailSelectSkuPopView *skuPopView ;

@end



@implementation ABSGoodsDetailViewController
//1： 创造数据源
-(NSMutableArray *)dataSource {
    if (!_dataSource) {
        
        _dataSource = [NSMutableArray array];
        
        /// 1: 获取x解析数据
        NSString *bundleString = [[NSBundle mainBundle] pathForResource:@"GoodsDetail" ofType:@"json"];
        
        NSData *jsonData = [NSData dataWithContentsOfFile:bundleString];
        
        
        NSDictionary *dict =  [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
  
        DetailRootModel *detailModel = [DetailRootModel mj_objectWithKeyValues:dict[@"data"]];
        
        DetailRootMvvmModel *frameModel = [[DetailRootMvvmModel alloc] init];
        
        frameModel.rootModel = detailModel;
        
        [_dataSource addObject:frameModel];
        

    }
    return _dataSource ;
}

//3: 创建bottomVIew
-(GoodsDetailBottomCartView *)bottomView  {
    if (!_bottomView) {
        _bottomView = [[GoodsDetailBottomCartView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 60, ScreenWidth, 50)];
        
        __weak __typeof (self) weakSelf = self ;
        
        _bottomView.backgroundColor = UIColor.grayColor;
        _bottomView.addCartBtnBlock = ^{
            /// 调用c弹窗的view信息
            NSLog(@"点击了添加购物车的按钮");
            [weakSelf.view addSubview:weakSelf.skuPopView];
            [weakSelf.skuPopView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.bottom.offset(0);
            }];
        
            
        };
    }
    return _bottomView;
}

//4: 创建SKUview
-(GoodsDetailSelectSkuPopView *)skuPopView {
    if (!_skuPopView) {
        _skuPopView = [[GoodsDetailSelectSkuPopView alloc]init];
        _skuPopView.backgroundColor =[UIColor colorWithHexString:@"#000000" alpha:0.6];
//        _skuPopView.alpha = 0.8 ;
        
        if (self.dataSource.count > 0) {
            _skuPopView.model = self.dataSource.firstObject ;
        }
    }
    return _skuPopView;
}

//2: 创建collectivVIew
-(UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 300);
        
        
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height ) collectionViewLayout:flowLayout];
        
        _collectionView.delegate = self ;
        _collectionView.dataSource = self ;
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        _collectionView.alwaysBounceVertical = YES ;
        _collectionView.showsVerticalScrollIndicator = NO ;
        _collectionView.showsHorizontalScrollIndicator = NO ;
        
        _collectionView.translatesAutoresizingMaskIntoConstraints = NO ;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellId];
        
        
        [_collectionView registerClass:[ABSGoodsDetailHeaerReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIndefine];

    }
    
    return _collectionView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =  @"商品详情";
    self.view.backgroundColor = UIColor.grayColor;
    
    [self.view addSubview:self.collectionView];

    [self.view addSubview:self.bottomView];
    [self.collectionView reloadData];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
     NSLog(@"====%@",self.dataSource);
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return  CGSizeMake(190, 180);
    
}

//4:titem 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return  UIEdgeInsetsMake(10, 10, 10, 10);

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *collectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellId forIndexPath:indexPath];
    
    collectionCell.backgroundColor = [UIColor redColor];
    return collectionCell ;
    
}


//4:headerView 的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    
    DetailRootMvvmModel *model = self.dataSource[section];
    
    return CGSizeMake(self.view.frame.size.width,  model.headerViewHeight);
}


//6： 获取header

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    ABSGoodsDetailHeaerReusableView  *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIndefine forIndexPath:indexPath];

    __weak typeof(self) weakSelf = self;
//    view.collectionImageBlock = ^(data * _Nullable dataModel) {
//        [weakSelf.navigationController pushViewController:[[TextChangeViewController alloc] init] animated:YES];
//    };
    
    DetailRootMvvmModel *model = self.dataSource[indexPath.section];
    view.model = model ;
    
//    view.model = self.dataSource[indexPath.section];
    return  view;
    
}

-(void)text3{
    /// 添加ScrollView信息
//    RootModel *model = self.dataSource.lastObject;
    
  
}


@end
